<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace app\assets;

use yii\web\AssetBundle;
use \yii\web\View;

/**
 * Base asset bundle.
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class BaseAsset extends AssetBundle
{

    public $basePath = '@webroot';
    public $baseUrl = '@web';

    /**
     * 定义按需加载JS方法  ，注意加载顺序在最后
     * @param \yii\web\View $view
     * @param $jsFile
     * @return string
     */
    public static function addScript($view, $jsFile)
    {

        $className = self::className();
        try {
            $view->registerJsFile($jsFile, [
                self::className(),
                'depends' => $className
            ]);
        } catch (\Exception $error) {
            // 记录下错误日志
            \Yii::error([
                "````````````````````````````````````````````````````````",
                "``               [Asset]静态资源加载失败                 ``",
                "`` 错误详情: {$className}下文件{$jsFile}加载失败         ``",
                "`` 错误信息和参数详情:                                    ``",
                "````````````````````````````````````````````````````````",
                $error->getTraceAsString()
            ], 'error');
        }
    }

    /**
     * 定义按需加载css方法，注意加载顺序在最后
     * @param \yii\web\View $view
     * @param $cssFile
     * @return string
     */
    public static function addCss($view, $cssFile)
    {

        $className = self::className();
        try {
            $view->registerCssFile($cssFile, [
                self::className(),
                'depends' => $className
            ]);
        } catch (\Exception $error) {
            // 记录下错误日志
            \Yii::error([
                "````````````````````````````````````````````````````````",
                "``               [Asset]静态资源加载失败                 ``",
                "`` 错误详情: {$className}下文件{$cssFile}加载失败         ``",
                "`` 错误信息和参数详情:                                    ``",
                "````````````````````````````````````````````````````````",
                $error->getTraceAsString()
            ], 'error');
        }
    }
}
